﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BuyThePeople.Composition
{
    public static class Utils
    {
        /// <summary>
        /// 문자열 또는 숫자를 타입으로 변환한다.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="origin"></param>
        /// <returns></returns>
        public static T ConvertToType<T> (object origin) 
        {
            T result = default(T);
            int n = 0;
            if (origin is int)
            {
                result = (T)origin;
            }
            else if ((origin is string)
                && (int.TryParse(origin.ToString(), out n) == true))
            {
                result = (T)(object)n;
            }
            return result;
        }

        public static int? ConvertToInt(object origin)
        {
            int? result = null;
            int n = 0;
            if (origin is int)
            {
                result = (int)origin;
            }
            else if ((origin is string)
                && (int.TryParse(origin.ToString(), out n) == true))
            {
                result = n;
            }
            return result;
        }
    }
}